﻿
using System;
using System.Collections.Generic;
using System.Linq;

namespace PTop.Domain.ViewModel.Bus
{
    /// <summary>
    /// 公交线路位置模型
    /// </summary>
    [Serializable]
    public class LineLocationModel
    {
        public int LineId { get; set; }

        /// <summary>
        /// 从始发站到终点站的线路经纬度列表
        /// </summary>
        public List<LocPoint> LocPoints { get; set; }

        /// <summary>
        /// 获取position从startPosition到endPosition的序列
        /// </summary>
        /// <param name="startPosition"></param>
        /// <param name="endPosition"></param>
        /// <returns></returns>
        public List<LocPoint> GetPartPoints(int startPosition, int endPosition)
        {
            var startPoint = LocPoints.First(m => m.IsStop && m.Position == startPosition);
            var startIndex = LocPoints.IndexOf(startPoint);
            var endPoint = LocPoints.First(m => m.IsStop && m.Position == endPosition);
            var endIndex = LocPoints.IndexOf(endPoint);
            var partList = LocPoints.Skip(startIndex).Take(endIndex - startIndex + 1).ToList();
            return partList;
        }

        /// <summary>
        /// 获取从startStopName到endStopName的序列
        /// </summary>
        /// <param name="startStopName"></param>
        /// <param name="endStopName"></param>
        /// <returns></returns>
        public List<LocPoint> GetPartPoints(string startStopName, string endStopName)
        {
            var startPoint = LocPoints.FirstOrDefault(m => m.IsStop && m.StopName == startStopName);
            if (startPoint == null) return null;
            var startIndex = LocPoints.IndexOf(startPoint);
            var endPoint = LocPoints.FirstOrDefault(m => m.IsStop && m.StopName == endStopName);
            if (endPoint == null) return null;
            var endIndex = LocPoints.IndexOf(endPoint);
            var partList = LocPoints.Skip(startIndex).Take(endIndex - startIndex + 1).ToList();
            return partList;
        }

        /// <summary>
        /// 站点序列
        /// </summary>
        /// <returns></returns>
        public List<LocPoint> GetStopLocPoints()
        {
            return LocPoints.Where(m => m.IsStop).ToList();
        }

        /// <summary>
        /// 新增站点
        /// </summary>
        /// <param name="position"></param>
        /// <param name="lng"></param>
        /// <param name="lat"></param>
        /// <param name="stopName"></param>
        public void AddStop(int position, double lng, double lat, string stopName)
        {
            var locPoint = new LocPoint
            {
                IsStop = true,
                Position = position,
                Lat = lat,
                Lng = lng,
                StopName = stopName
            };
            var oldLoc = LocPoints.FirstOrDefault(m => m.Position == position);
            if (oldLoc == null)
            {
                LocPoints.Add(locPoint);
            }
            else
            {
                var index = LocPoints.IndexOf(oldLoc);
                foreach (var lp in LocPoints)
                {
                    if (lp.IsStop && lp.Position >= position)
                    {
                        lp.Position++;
                    }
                }
                LocPoints.Insert(index, locPoint);
            }
        }

        /// <summary>
        /// 移除站点
        /// </summary>
        /// <param name="position"></param>
        public void RemoveStop(int position)
        {
            LocPoints.RemoveAll(m => m.Position == position);
            foreach (var lp in LocPoints)
            {
                if (lp.IsStop && lp.Position > position)
                {
                    lp.Position--;
                }
            }
        }

        /// <summary>
        /// 移除站点
        /// </summary>
        /// <param name="stopName"></param>
        public void RemoveStop(string stopName)
        {
            var point = LocPoints.FirstOrDefault(m => m.StopName == stopName);
            if (point == null) return;
            RemoveStop(point.Position);
        }

        /// <summary>
        /// 修改坐标值
        /// </summary>
        /// <param name="stopName"></param>
        /// <param name="lng"></param>
        /// <param name="lat"></param>
        public void UpdateLngLat(string stopName, double lng, double lat)
        {
            var point = LocPoints.FirstOrDefault(m => m.StopName == stopName);
            if (point == null) return;
            point.Lng = lng;
            point.Lat = lat;
        }
    }

    public class LocPoint
    {
        public bool IsStop { get; set; }
        public int Position { get; set; }
        public double Lng { get; set; }
        public double Lat { get; set; }
        public string StopName { get; set; }
    }
}
